//
//  LWPeixunListCtrl.m
//  laowu
//
//  Created by 尹桥印 on 15/5/2.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "LWPeixunListCtrl.h"
#import "PeixunItenCell.h"

@interface LWPeixunListCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *itemArray;
@property (nonatomic,assign)BOOL isDelete;//是否是“删除”的编辑模式
@property (weak, nonatomic) IBOutlet UIButton *allSelectBtn;

@end

@implementation LWPeixunListCtrl

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.bottomView setHidden:YES];
    [self setTitle:@"培训申请"];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setTableFooterView:[UIView new]];
    
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView headerBeginRefreshing];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unSelected) name:@"unselected" object:nil];
    
}

-(void)headerRefresh{
    [self requestDataWithMaxId:0];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if (self.itemArray.count) {
        PeiXunResult *item = [self.itemArray lastObject];
        maxId = item.id-1;
    }
    [self requestDataWithMaxId:maxId];
}

-(void)requestDataWithMaxId:(NSInteger)maxId{
    [MeDataTool getPeiXunListWithMaxId:maxId success:^(PeiXunRes *result) {
        [Utils refreshComplete:self.tableView];
        if (result.isSucc) {
            //加载成功
            //判断maxid是否为0
            if (maxId==0) {
                //清空原有集合
                [self.itemArray removeAllObjects];
            }
            [self.itemArray addObjectsFromArray:result.datas];
            [_tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTipsWithView:nil String:@"加载失败"];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PeixunItenCell *cell = [[PeixunItenCell alloc] initWithTabelView:tableView indexPath:indexPath];
    [cell setPeiXunItem:self.itemArray[indexPath.row]];
    [cell.selectBtn setHidden:!self.isDelete];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 128;
}

-(void)edit{
    self.isDelete = !self.isDelete;
    if (!self.isDelete) {
        [self.navigationItem.rightBarButtonItem setTitle:@"删除"];
    }else{
        [self.navigationItem.rightBarButtonItem setTitle:@"取消"];
    }
    [self.bottomView setHidden:!self.isDelete];
    [self.tableView reloadData];
}

-(void)unSelected{
    [self.allSelectBtn setSelected:NO];
}

- (IBAction)allSelect:(UIButton *)sender {
    [sender setSelected:!sender.selected];
    [self.itemArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        PeiXunResult *msg = obj;
        [msg setSelected:sender.selected];
    }];
    [self.tableView reloadData];

}
- (IBAction)deleteBtnClick:(id)sender {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selected = 1"];
    NSArray *resultArray = [self.itemArray filteredArrayUsingPredicate:predicate];
    
    if (!resultArray.count) {
        return;
    }
    
    NSMutableArray *idArray = [NSMutableArray array];
    for (PeiXunResult *result in resultArray) {
        [idArray addObject:@(result.id)];
    }
    [MeDataTool deleteTrainApplyWithIdArray:idArray success:^(BaseResponse *result) {
        if (result.isSucc) {
            [DialogUtils showTipsWithView:nil String:@"删除成功"];
            [self.itemArray removeObjectsInArray:resultArray];
            [self.tableView reloadData];
        }else{
            [DialogUtils showTipsWithView:nil String:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTipsWithView:nil String:@"删除失败"];
    }];
    
    
}
@end
